using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 外观管理器
/// </summary>
public class AppearanceManager : MonoBehaviour
{ 
    private static AppearanceManager _instance;

    public static AppearanceManager Instance => _instance;

    private void Awake()
    {
        _instance = this;
        InitManager();
    }

    private void Start()
    {
        
    }

    private const int APPEARANCE_HEAD_COUNT = 4;

    public PlayerAppearance playerAppearance;

    [SerializeField]
    private List<AppearancePart> headAppearancesList;//头部外观资源列表
    private int headAppearanceIndex = 0;//头部外观索引

    public void InitManager()
    {
        if (playerAppearance == null)
        {
            playerAppearance = FindObjectOfType<PlayerAppearance>();
        }

        headAppearancesList = new List<AppearancePart>();
        for (int i = 0; i < APPEARANCE_HEAD_COUNT; i++)
        {
            string path = "Head/Head_" + i;
            var res = Resources.Load<AppearancePart>(path);

            headAppearancesList.Add(res);
        }
    }

    private void OnGUI()
    {
        if (playerAppearance == null)
        {
            return;
        }

        if (GUILayout.Button("NextHead", GUILayout.Width(200), GUILayout.Height(100)))
        {
            NextHead();
        }
    }

    public void NextHead()
    {
        headAppearanceIndex++;
        if (headAppearanceIndex >= headAppearancesList.Count)
        {
            headAppearanceIndex = 0;
        }

        var newPartRes = headAppearancesList[headAppearanceIndex];
        var newPartIns = Instantiate(newPartRes);
        playerAppearance.ReplaceAppearancePart(newPartIns);
    }
}
